package com.xbao.service.impl;

import com.github.pagehelper.PageHelper;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.dao.ClsExtraMapper;
import com.xbao.dao.ClsMapper;
import com.xbao.dao.StuClsRelMapper;
import com.xbao.model.Cls;
import com.xbao.model.ClsExample;
import com.xbao.model.StuClsRel;
import com.xbao.model.StuClsRelExample;
import com.xbao.model.req.ClsReq;
import com.xbao.model.req.ResultAddition;
import com.xbao.model.vo.ClsVo;
import com.xbao.model.vo.EmpVo;
import com.xbao.model.vo.RoomVo;
import com.xbao.model.vo.SubjectVo;
import com.xbao.service.ClsService;
import com.xbao.service.EmpService;
import com.xbao.service.RoomService;
import com.xbao.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/24.
 */
@Service
public class ClsServiceImpl implements ClsService {
    @Resource
    private ClsMapper clsMapper;
    @Autowired
    private ClsExtraMapper clsExtraMapper;
    @Autowired
    private StuClsRelMapper relMapper;

    @Resource
    private EmpService empService;

    @Resource
    private SubjectService subjectService;
    @Resource
    private RoomService roomService;

    @Override
    public List<ClsVo> getClsOfStu(Long sid) {


        return null;


    }

    @Override
    public List<StuClsRel> getClsRelOfStu(Long sid) {
        StuClsRelExample example = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = example.createCriteria();
        criteria.andSidEqualTo(sid);
        List<StuClsRel> rels = relMapper.selectByExample(example);

        return rels;
    }


    /**
     * @param cls
     */
    @Override
    public void addCls(Cls cls) {
        clsMapper.insert(cls);
    }


    @Override
    public ClsVo detailCls(ClsReq req) {
        Cls cls = clsMapper.selectByPrimaryKey(req.getId());
        ClsVo vo = MyBeanUtil.copyProperties(cls, ClsVo.class);
        if(req.getRadd()!=null&&req.getRadd().getIallrecord()==true){
            finishCount(vo);
        }
        return vo;





    }
    public void  finishCount(ClsVo vo){
        HashMap<String,Integer> map=clsExtraMapper.finishRecordCount(vo.getId());
        if(map!=null){
            vo.setTotalRecords(map.get("totalRecords"));
            vo.setFinishRecords(map.get("finishRecords"));
            vo.setRemainRecords(map.get("remainRecords"));
        }
    }




    /**
     * @param id
     */
    @Override
    public void deleteCls(Long id) {
        clsMapper.deleteByPrimaryKey(id);
    }

    /**
     * @param req
     * @return
     */
    @Override
    public List<ClsVo> queryCls(ClsReq req) {
        ClsExample clsExample = new ClsExample();
        ClsExample.Criteria criteria = clsExample.createCriteria();
        if (req.getPager() != null) {
            PageHelper.startPage(req.getPager().getPageNo(), req.getPager().getPageSize());
            clsExample.setOrderByClause("id desc");
        }
        if (req.getType() != null) {
            criteria.andTypeEqualTo(req.getType());
        }
        if (req.getOrgid() != null) {
            criteria.andOrgidEqualTo(req.getOrgid());

        }
        List<Cls> clss = clsMapper.selectByExample(clsExample);
        List<ClsVo> vos = MyBeanUtil.copyList(clss, ClsVo.class);
        resultAdd(vos, req.getRadd());
        return vos;
    }


    public void resultAdd(List<ClsVo> vos, ResultAddition resultAddition) {
        if(resultAddition==null) return;
        for (ClsVo vo : vos) {
            if (resultAddition.getIename()) {
                EmpVo ev = empService.detailEmp(vo.getEid());
                if (ev != null)
                    vo.setEname(ev.getName());
            }
            if(resultAddition.getIcname()){

            }
            if(resultAddition.getIroomname()){
                RoomVo room=roomService.detailRoom(vo.getRid());
                if(room!=null)
                    vo.setRoomname(room.getName());
            }
            if(resultAddition.getIsname()){
                SubjectVo subjectVo=subjectService.detailSubject(vo.getSid());
                if(subjectVo!=null)
                vo.setSubname(subjectVo.getName());
            }


        }


    }


    /**
     * @param req
     * @return
     */
    @Override
    public List<ClsVo> queryClsPage(ClsReq req) {
        ClsExample example = new ClsExample();
        ClsExample.Criteria criteria = example.createCriteria();
        if (req.getType() != null) {
            criteria.andTypeEqualTo(req.getType());
        }
        List<Cls> clss = clsMapper.selectByExample(example);
        List<ClsVo> vos = MyBeanUtil.copyList(clss, ClsVo.class);
        return vos;
    }

    /**
     * @param clsReq
     */
    @Override
    public void updateCls(ClsReq clsReq) {
        clsMapper.updateByPrimaryKeySelective(clsReq);

    }

    /**
     * @param
     * @return
     */


    @Override
    public void finish(ClsReq req) {
        ClsReq temp = new ClsReq();
        temp.setId(temp.getId());
        temp.setStatus(2);
        clsMapper.updateByPrimaryKeySelective(req);

    }


    public void joinCls(List<StuClsRel> clsReqs) {
        for (StuClsRel rel : clsReqs) {
            joinCls(clsReqs);
        }

    }

    @Override
    public void joinCls(StuClsRel clsReq) {
        relMapper.insert(clsReq);

    }

    @Override
    public void leaveCls(StuClsRel clsReq) {
        StuClsRelExample example = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = example.createCriteria();
        criteria.andSidEqualTo(clsReq.getSid());
        criteria.andCidEqualTo(clsReq.getCid());
        relMapper.deleteByExample(example);


    }

}
